using System.Collections;
using System.Collections.Generic;
using UnityEngine;


namespace TowerDefence
{
    public class MapManager
    {
        private const string mMapPath = "Map/";
        
        private static GameObject mMap;

        public static List<GameObject> mMapRunBoxList = new List<GameObject>();

        public static GameObject Map
        {
            get => mMap;
            
            set=>mMap = value;
        }

        public static List<Vector3> GetMapPath(string mapName)
        {
           var mapText= Resources.Load<TextAsset>(mMapPath + mapName);
            string text = mapText.text;
            string []posStrings = text.TrimEnd('\n').Split('\n');
            
           List<Vector3> mapPath = new List<Vector3>();
           string[] posXZ;
           for (int i = 0; i < posStrings.Length; i++)
           {
               posXZ = posStrings[i].Split(',');
               mapPath.Add(new Vector3(int.Parse(posXZ[0]), 0,int.Parse(posXZ[1])));
           }

           for (int i = 0; i<mMapRunBoxList.Count; i++)
           {
               GameObject.Destroy(mMapRunBoxList[i]);
           }
           mMapRunBoxList.Clear();

           if (mapPath.Count > 0)
           {
               GameObject boxObj = Resources.Load<GameObject>("Model/map_box");
               Vector3 pos = mapPath[0];
               GameObject box;
                   
               box= GameObject.Instantiate(boxObj);
               box.transform.localPosition = pos;
               mMapRunBoxList.Add(box);
               for (int i = 0; i < mapPath.Count; i++)
               {
                   while (pos.x>mapPath[i].x)
                   {
                       pos.x--; 
                       box = GameObject.Instantiate(boxObj);
                       box.transform.localPosition = pos;
                       mMapRunBoxList.Add(box);
                   }
                   while (pos.x<mapPath[i].x)
                   {
                       pos.x++;
                       box = GameObject.Instantiate(boxObj);
                       box.transform.localPosition = pos;
                       mMapRunBoxList.Add(box);
                   }
                   while (pos.z<mapPath[i].z)
                   {
                       pos.z++;
                       box = GameObject.Instantiate(boxObj);
                       box.transform.localPosition = pos;
                       mMapRunBoxList.Add(box);
                   }
                   while (pos.z>mapPath[i].z)
                   {
                       pos.z--;
                       box = GameObject.Instantiate(boxObj);
                       box.transform.localPosition = pos;
                       mMapRunBoxList.Add(box);
                   }
               }
           }
           
           
           return  mapPath; 
                

        }


        public static void ClearMapBox()
        {
            for (int i = 0; i < mMapRunBoxList.Count; i++)
            {
                GameObject.Destroy(mMapRunBoxList[i]);
            }
            mMapRunBoxList.Clear();
        }
    }
}

